package com.dd.edu.oss.service;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import com.dd.edu.oss.bean.UpLoadResult;
import com.dd.edu.oss.config.AliyunConfig;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.UUID;

@Service
public class OSSService {

    @Autowired
    private AliyunConfig aliyunConfig;
    @Autowired
    private OSSClient ossClient;

    private static final String[] IMAGE_TYPE = new String[]{".jpg",".jpeg",".png"};


    public UpLoadResult upload(MultipartFile multipartFile){
        boolean isOK =false;
        for (String type : IMAGE_TYPE) {
            if (StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(),type)) {
                isOK = true;
                break;
            }
        }

        UpLoadResult upLoadResult = new UpLoadResult();
        if (!isOK) {
            upLoadResult.setStatus("图片类型不对");
            return upLoadResult;
        }

//        大小检查：必须小于5M，大于5M时返回错误提示信息 5*1024*1024
        if(multipartFile.getSize() > 5*1024*1024){
            upLoadResult.setStatus("图片超过5M");
            return upLoadResult;
        }


        String originalFilename = multipartFile.getOriginalFilename();
        String filePath = getFilePath(originalFilename);
        try {
            PutObjectResult putObjectResult = ossClient.putObject(aliyunConfig.getBucketName(), filePath, new ByteArrayInputStream(multipartFile.getBytes()));

        } catch (IOException e) {
            e.printStackTrace();
            upLoadResult.setStatus("error");
            return upLoadResult;
        }

        upLoadResult.setStatus("done");
        upLoadResult.setName(this.aliyunConfig.getUrlPrefix() + filePath);
        upLoadResult.setUid(String.valueOf(System.currentTimeMillis()));
        return upLoadResult;
    }

    public InputStream download(String filePath){
        // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
        OSSObject ossObject = ossClient.getObject(aliyunConfig.getBucketName(), filePath);

        return ossObject.getObjectContent();
    }

    public boolean delete(String filePath){
        boolean found = ossClient.doesObjectExist(aliyunConfig.getBucketName(), filePath);
        if(found) {
            ossClient.deleteObject(aliyunConfig.getBucketName(), filePath);
            return true;
        }else {
            return false;
        }
    }


    private String getFilePath(String sourceFileName) {
        DateTime dateTime = new DateTime();
        return "images/" + dateTime.toString("yyyy") + "/" + dateTime.toString("MM") + "/"
                + dateTime.toString("dd") + "/" + UUID.randomUUID().toString() + "."
                + StringUtils.substringAfterLast(sourceFileName, "."); }
}
